import http, { AppHttp } from "@/http/axios_config";
import AppConstant from "../common/AppConstant";
import { PageRequest } from "../common/AppReq";

export interface AiDocStreamChatRequest {
  prompt: string,
  conversationId: string
}

/**
 * 对话组件支持的消息数据结构
 */
export interface AiChatMessage {
  content: string,
  createAt: number,
  id: string,
  role: string,
  updateAt: number
}

// {
//   content: `我怎么知道我是谁！`,
//   createAt: 1697862242458,
//   id: 'Sb5pAzLL',
//   role: 'assistant',
//   updateAt: 1697862243540,
// },

/**
 * 流式问答请求
 */
async function aiDocStreamChat(request: AiDocStreamChatRequest, config?: any) : Promise<Response> {
  if (config) {
    // TODO other config
  }
  // 使用fetch。后端接口可能会变为post，eventsource有局限性
  let res = AppHttp.fetchHttpWithStream(AppConstant.UMI_BASE_URL + "/api/ai-doc/chat-stream", {
    method: "POST",
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
    body: JSON.stringify(request),
  })
  res.catch(err => console.log(err))
  return res;
} 

export interface QueryConversationListReq extends PageRequest{
  conversationId?: string,
  conversationIdList?: string[],
  tilte?: string
}

export interface QueryConversationResp {
  id: string
  title?: string
  description?: string 
  create_time?: number
  update_time?: number
  // 用到自己解构
  creator?: any
  conversationType?: string
}

export interface QueryConversationListResp {
  total: number,
  data: QueryConversationResp[]
}

/**
 * 加载历史对话列表
 */
async function loadHistoryConversation(request: QueryConversationListReq, config?: any): 
  Promise<QueryConversationListResp | null> {
  try {
    let resp: QueryConversationListResp = await http.post("/api/ai-doc/query-conversation-list", {...request})
    if (resp === null) {
      return null;
    }
    console.log("conversation data: ", resp)
    return resp
  } catch (err) {
    console.log(err)
    return null;
  }
}


export interface LoadHistoryMessage {
  id: string
}
/**
 * 
 */
async function loadHistoryConversationMessage(request: LoadHistoryMessage, config?: {}) {
  try {
    const resp: any = await http.get("/api/ai-doc/query-chat-message/" + request.id)
    return resp?.data?.map((item: any) => {
      return {...item, createAt: item.create_time, updateAt: item.update_time} as AiChatMessage
    })
  } catch (err) {
    console.log(err)
    return null
  }
}

async function updateConversationInfo(request: any, config?: any) {
  try {
    await http.post("/api/ai-doc/update-conversation-info", {...request})
  } catch (err) {
    console.log("")
  }
}


async function createConversationInfo(request: any, config?: any) {
  try {
    await http.post("/api/ai-doc/create-conversation", {...request})
  } catch (err) {
    console.log("")
  }
}

async function deleteConversationInfo(request: any, config?: any) {
  try {
    await http.get("/api/ai-doc/delete-conversation/" + request.id)
  } catch (err) {
    console.log("")
  }
}

const AiDocService = {
  aiDocStreamChat,
  loadHistoryConversation,
  loadHistoryConversationMessage,
  updateConversationInfo,
  createConversationInfo,
  deleteConversationInfo,
}

export default AiDocService;